package com.fairy.sso.jedis;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.fairy.sso.config.FairyConfig;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
 
/**
 * Redis 使用
 * @author caspar
 * http://blog.csdn.net/tuposky
 */

public class JedisUtil {

	@Autowired(required=true)
	private FairyConfig config;
	
	private ShardedJedisPool shardedJedisPool;
	private ShardedJedis shardedJedis;
	
	@SuppressWarnings("resource")
	public  ShardedJedis  getRedisClient()
	{
		 List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
		 
		 String redisIp=config.getRedisIp();
		 if(redisIp.contains(","))
		 {
			 String[]arr=redisIp.split(",");
			 for(String ips:arr)
			 {
				 String[]array=ips.split(":");
				 String ip=array[0];
				 int port=Integer.valueOf(array[1]);
				 shards.add(new JedisShardInfo(ip, port));
			 }
		 }
		 else
		 {
			 String[]array=redisIp.split(":");
			 String ip=array[0];
			 int port=Integer.valueOf(array[1]);
			 shards.add(new JedisShardInfo(ip, port));
		 }
	     //new JedisShardInfo("192.168.120.51", 6379, "master");
	 
	     ShardedJedisPool sjp = new ShardedJedisPool(new JedisPoolConfig(), shards);
	     ShardedJedis shardClient = sjp.getResource();
	     

	     
	        /**
	        try {
	            shardClient.set("A", "123");
	            shardClient.set("B", "234");
	            shardClient.set("C", "345");
	 
	            try {
	                System.out.println(shardClient.get("A"));
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	 
	            try {
	                System.out.println(shardClient.get("B"));
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	 
	            try {
	                System.out.println(shardClient.get("C"));
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        } finally {
	            sjp.returnResource(shardClient);
	        }*/
		return shardClient;
	}
	
    public JedisUtil() {
        //initialPool();
        initialShardedPool();
        shardedJedis = shardedJedisPool.getResource();
        //jedis = jedisPool.getResource();
    }
	  private void initialShardedPool() {
	   // 
	  JedisPoolConfig config = new JedisPoolConfig();
	  //config.setMaxActive(20);
	  config.setMaxTotal(20);
	  config.setMaxIdle(5);
	  config.setMaxWaitMillis(1000l);
	  //config.setMaxWait(1000l);
	  config.setTestOnBorrow(false);
	   // slave
	  List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
	  shards.add(new JedisShardInfo("192.168.120.51", 6379, "master"));

	   shardedJedisPool = new ShardedJedisPool(config, shards);
	  }
	  
	  @SuppressWarnings("deprecation")
	public void set(String key,String value)
	  {
		  
		  
		  shardedJedis.set(key, value);
		  shardedJedisPool.returnResource(shardedJedis);
	  }
	  
	  public String get(String key,String value)
	  {
		  String str=shardedJedis.get(key);
		  shardedJedisPool.returnResource(shardedJedis);
		  return str;
		  
	  }
	 
}
